<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" />
<xsl:template match="/">
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
<xsl:for-each select="Schema/ForeignKeys/ForeignKey">
<xsl:if test="/Schema/@IsDropCode='1'">
if exists (select * from dbo.sysobjects where id = object_id(N'[_<xsl:value-of select="/Schema/@TableName" />SelectBy<xsl:for-each select="Column"><xsl:value-of select="FKCOLUMN_NAME" /><xsl:if test="position()!=last()">AND</xsl:if></xsl:for-each>]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [_<xsl:value-of select="/Schema/@TableName" />SelectBy<xsl:for-each select="Column"><xsl:value-of select="FKCOLUMN_NAME" /><xsl:if test="position()!=last()">AND</xsl:if></xsl:for-each>]
GO
</xsl:if>
--根据外键获取<xsl:value-of select="/Schema/@TableName" />表记录。请勿修改此存储过程，如果需要特殊的选择方法，请另写存储过程。
CREATE PROCEDURE _<xsl:value-of select="/Schema/@TableName" />SelectBy<xsl:for-each select="Column"><xsl:value-of select="FKCOLUMN_NAME" /><xsl:if test="position()!=last()">AND</xsl:if></xsl:for-each>
	(<xsl:for-each select="Column">
	@<xsl:value-of select="concat(FKCOLUMN_NAME,' ',SystemTypeName)" /><xsl:if test="IsLengthDef=1">(<xsl:value-of select="Length" />)</xsl:if><xsl:if test="position() != last()">,</xsl:if>
	</xsl:for-each>
	)<xsl:if test="/Schema/@IsEncrypt='1'">
	WITH ENCRYPTION
	</xsl:if>
AS
SELECT * FROM [<xsl:value-of select="/Schema/@Owner" />].[<xsl:value-of select="/Schema/@TableName" />] <xsl:if test="Schema/@IsNoLock='1'">  WITH(NOLock)</xsl:if>
WHERE<xsl:for-each select="Column">
	[<xsl:value-of select="FKCOLUMN_NAME" />]=@<xsl:value-of select="FKCOLUMN_NAME" /><xsl:if test="position()!=last()"> AND </xsl:if>
	</xsl:for-each>
GO

</xsl:for-each>

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
</xsl:template>
</xsl:stylesheet>